<template>
  <div class="app-container org">
    <el-card>
      <el-row>
        <el-col :span="24">
          <p class="pinformation">搜索条件</p>
        </el-col>
      </el-row>
      <el-form ref="queryRef" label-width="100" :model="queryParams">
        <el-row>
          <el-col :span="3">
            <el-form-item label="自制样品名称：" prop="name">
              <el-input clearable maxlength="100" placeholder="请输入自制样品名称" v-model="queryParams.name"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="唯一性编号：" prop="code">
              <el-input clearable maxlength="100" placeholder="请输入唯一性编号" v-model="queryParams.code"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="配置人：" prop="selfAdminId" label-width="60">
              <el-select v-model="queryParams.selfAdminId" clearable filterable>
                <el-option v-for="item in proxy.addListNoLimit(userList, 'userId', 'nickName')" :key="item.userId"
                  :value="item.userId" :label="item.nickName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="配置日期：">
              <el-date-picker value-format="YYYY-MM-DD" type="daterange" v-model="dateRange" range-separator="-"
                start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item label="领用人：" prop="createUserId" label-width="60">
              <el-select clearable v-model="queryParams.createUserId">
                <el-option v-for="dict in proxy.addListNoLimit(userList, 'userId', 'nickName')" :key="dict.userId"
                  :label="dict.nickName" :value="dict.userId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="领用日期：" label-width="110">
              <el-date-picker value-format="YYYY-MM-DD" type="daterange" v-model="dateRange1" range-separator="-"
                start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-row justify="center">
              <el-radio label="0" v-model="queryParams.isLike">模糊搜索</el-radio>
              <el-radio label="1" v-model="queryParams.isLike">精准搜索</el-radio>
              <el-button type="success" @click="handleQuery">搜索</el-button>
              <el-button class="resetBtn" @click="resetQuery">重置</el-button>
            </el-row>
          </el-col>
        </el-row>
      </el-form>
      <el-row>
        <p class="pinformation">自制样品列表</p>
        <p class="totalStyle">共 {{ total }} 条数据</p>
        <el-table ref="mainTableRef" :data="recordTable" style="margin-top: 5px" @selection-change="handleSelChange">
          <el-table-column label="唯一性编号" prop="selfSample.sampleCode" align="center" />
          <el-table-column label="自制样品名称" prop="selfSample.sampleName" align="center" />
          <el-table-column label="种类" prop="selfSample.sampleType" align="center">
            <template #default="scope">
              <dict-tag :options="sys_self_sample_type" :value="scope.row.selfSample.sampleType" />
            </template>
          </el-table-column>
          <el-table-column label="溶剂" prop="selfSample.rj" align="center">
          </el-table-column>
          <el-table-column label="定容体积" prop="selfSample.drtj" align="center">
            <template #default="scope">
              <span v-html="scope.row.selfSample.drtj +
                transformation(scope.row.selfSample.drtjUnitName)
                "></span>
            </template>
          </el-table-column>
          <el-table-column label="配置日期" prop="createDate" align="center" />
          <el-table-column label="有效日期" prop="selfSample.yxDate" align="center" />
          <el-table-column label="配置人" prop="selfAdminId" align="center">
            <template #default="scope">
              {{
                proxy.getItemByArray(
                  userList,
                  "userId",
                  scope.row.createUserId,
                  "nickName"
                )
              }}
            </template>
          </el-table-column>
          <el-table-column label="检测项目数" prop="selfSample.jcxmCount" align="center">
            <template #default="scope">
              <el-button link @click="jcxmCountClick(scope.row.sampleId)">{{
                scope.row.selfSample.jcxmName ? scope.row.selfSample.jcxmName : scope.row.selfSample.jcxmCount
              }}</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-row>
    </el-card>
    <!-- 点击行内检测项目数的弹窗 -->
    <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
      v-model="jcxmOpen" @close="closedDialog" :title="title">
      <el-row>
        <p class="pinformation">检测项目列表</p>
        <p class="totalStyle">共 {{ multiplexTable.length }} 条数据</p>
      </el-row>
      <el-table :data="multiplexTable">
        <el-table-column label="检测项目" prop="bucketId" align="center">
          <template #default="scope">
            <span v-html="transformation(scope.row.jcxmName)"></span>
          </template>
        </el-table-column>
        <el-table-column label="浓度值" prop="con" align="center" />
        <el-table-column label="浓度值单位" prop="conUnit" align="center">
          <template #default="scope">
            <span v-html="transformation(scope.row.conUnitName)"></span>
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="danger" @click="jcxmOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BzwzRecord">
import {
  getSampleOutList,
  sampleList,
  getJcxmList,
} from "@/api/resource/selfSampleLedger";
import { getUnitList } from "@/api/store/unit";
import { listUser } from "@/api/contract/check";

const { proxy } = getCurrentInstance();
const {
  sys_self_sample_type,
} = proxy.useDict(
  "sys_self_sample_type",
);

const recordTable = ref([]);
const multiplexTable = ref([]);
const jcxmList = ref([]);
const title = ref("查看检测项目");
const jcxmOpen = ref(false);
const total = ref(0);
const dateRange = ref([]);
const dateRange1 = ref([]);
const data = reactive({
  queryParams: {
    pageNum: 1, // 当前页数
    pageSize: 10, // 当前页数据条数
    selfSample: {},
    isLike: "0",
    selfAdminId: -1,
    createUserId: -1
  },
});

const { queryParams } = toRefs(data);

const heightParams = ref([{ tableName: "main", bottomHeight: 21 }]);

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}

getJcxmList().then((res) => {
  jcxmList.value = res.data;
});
onMounted(() => {
  nextTick(() => {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  })
})
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

function getList() {
  let temp = {};
  temp = {
    "selfSample.sampleName": queryParams.value.name,
    "selfSample.sampleCode": queryParams.value.code,
    "selfSample.selfAdminId": queryParams.value.selfAdminId,
    "selfSample.isLike": queryParams.value.isLike,
    createUserId: queryParams.value.createUserId,
    pageSize: queryParams.value.pageSize,
    pageNum: queryParams.value.pageNum,
  };
  temp.params = {
    beginTime1: dateRange1.value[0],
    endTime1: dateRange1.value[1],
    beginTime: dateRange.value[0],
    endTime: dateRange.value[1],
  };
  getSampleOutList(temp).then((res) => {
    recordTable.value = res.rows;
    total.value = res.total;
  });
}
getList();

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  dateRange.value = [];
  dateRange1.value = [];
  proxy.resetForm("queryRef");
  queryParams.value.isLike = "0"
  queryParams.value.selfSample = {},
    handleQuery();
}

function jcxmCountClick(row) {
  sampleList({ sampleId: row }).then((res) => {
    multiplexTable.value = res.data;
    jcxmOpen.value = true;
  });
}

function closedDialog() {
  multiplexTable.value = [];
}

const unitList = ref([]);
getUnitList({ unitGroup: 0 }).then((res) => {
  unitList.value = res.data;
});

function handleSelChange(selection) {
  multiplexTable.value = selection;
}

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "main", bottomHeight: 73 }];
    } else {
      heightParams.value = [{ tableName: "main", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

function transformation(row) {
  const a = proxy.getSupAndSub(row);
  return a;
}

const userList = ref([]);
listUser().then((res) => {
  userList.value = res.data;
});
</script>

<style lang="scss" scoped>
.el-radio {
  margin-right: 5px;
}
</style>
